const express = require("express");

let router = express.Router();

/*
{
  framework: '8',
  projectType1: '20',
  pname: '美的返修订单系统',
  pdesc: 'IHRM是一款人资',
  pframework: 'Vue2全家',
  pmodel_arr: [
    { pmodel: '登录', pmodeldesc: '登录使用vuex' },
    { pmodel: '权限', pmodeldesc: 'RBAC' }
  ]
}
*/  
router.post("/postProject", async (req, res) => {

    // console.log(req.body);
    /*
        framework:框架id
        projectType1:项目大类id
        pname：项目名称
        pdesc：项目描述
        pframework：项目
        pmodel_arr: 项目模块+职责描述数组
    */ 
    let {framework,projectType1,pname,pdesc,pframework,pmodel_arr} = req.body;
    try{
    // 1.0 将项目名字插入category表
    let insertSql1 = `INSERT INTO category(cName,typeValue,typeTag,typeName,pid,sortno,isdelete) 
                    VALUES ('${pname}', NULL, 'projecttype2', '项目类型2级-录入', ${projectType1}, 999, 0);`

    let res1 = await res.execSql(insertSql1);
    let cateid = res1.insertId; // 获取category表插入的数据id

    // 2.0 插入项目描述
    let insertSql2 = ` insert into project_desc(projectDesc,cid,fid,isdelete)
                    values ('${pdesc}',${cateid},${framework},0)`;
    let res2 = await res.execSql(insertSql2);

    // 3.0 插入技术栈描述
    let insertSql3 = ` insert into technologygroup(\`desc\`,frameworkid,projecttypeid,isdelete)
                      values('${pframework}',${framework},${projectType1},0) `;
    let res3 = await res.execSql(insertSql3);

    // 4.0 项目模块和职责
    pmodel_arr.forEach(async item => {
        // 4.1 插入项目模块
        let insertSql4 = ` insert into project_model(mname,mremark,cid,isdelete)
                            values('${item.pmodel}','${pname}',${cateid},0) `;
         let res4 = await res.execSql(insertSql4);
         let modelid = res4.insertId; // 获取模块id

         let insertSql5 = ` insert into model_desc(mid,\`desc\`,isdelete) 
                            values(${modelid},'${item.pmodeldesc}',0) `;
         let res5 = await res.execSql(insertSql5);
    });

    res.send({ code: 200, msg: `项目保存成功`,data:{msg:'项目保存成功',cateid }});

    }catch(err){
        res.send({ code: 500, msg: err });
    }
});

// 导出
module.exports = router;
